// pages/detail/index.js
var WxParse = require('../../wxParse/wxParse.js');
var common= getApp().common;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showMotal: false,
        good: {},
        skuId: 0,
        buyurl: "",
        from_uid: 0,
        isDetailShow:true,
        isDetailContentShow:false,
        memberid: 0,
        globalset: {},
        isshow_subsidy:true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.init();
       
    },
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {


    },
    

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
      
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.onLoad();
    },
    
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
           
        }
        var globalset = wx.getStorageSync('globalset');
        var memberinfo = wx.getStorageSync('memberinfo');
        var total_commission = 0;
        //计算总的佣金比例
        for (var i = 0; i < globalset.memberlevel.length; i++) {
           
          if (globalset.memberlevel[i].type == memberinfo.type && globalset.memberlevel[i].name == memberinfo.level) {
            var total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2) + parseFloat(globalset.memberlevel[i].myleader1_credit2) + parseFloat(globalset.memberlevel[i].myleader2_credit2);
            //换成只取1级佣金的比例
            total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2);
           
            
           
          }
        }
     
        if (total_commission == 0) {
            this.data.good.wlPrice_after1 = this.data.good.discount;
        } else {
          this.data.good.wlPrice_after1 = this.data.good.wlPrice_after * (this.data.good.wlCommissionShare / 100)* (total_commission / 100)*0.9;
         


        }

        if (this.data.good.wlPrice_after1 == null || this.data.good.wlPrice_after1=="null"){
          this.data.good.wlPrice_after1=0;
        }

        if (this.data.good.wlPrice_after1 > 0)
           this.data.good.wlPrice_after1 =parseFloat(this.data.good.wlPrice_after1).toFixed(2)
         

        var title1 = "【原价】￥" + this.data.good.wlPrice + "【券后价】￥" + this.data.good.wlPrice_after + " " + this.data.good.skuName;

        return {
            title: title1,
            path: '/haojk/pages/detail/index?skuId=' + this.data.skuId + '&from_uid=' + this.data.memberid,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },

    init:function(){
      var skuid=this.options.skuid;
      var that=this;
      that.lookDetail();
      var option={
        apiHost:'https://jd.open.apith.cn/union/',
        api:'getGoodsInfo',
        data:{skuIds:skuid},
        successCode: 1,
        fn:function(data){
            that.setData({good:data[0]});
        },
      }
      common.request(option);
    },


    openSearch: function () {
       
        wx.showModal({
            title: '确认操作',
            content: '是否使用 超级搜索 查询本商品更多优惠',
            success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                        url: "../search/index",

                    })
                } else if (res.cancel) {
                  
                }
            }
        })
    },




    
    //返回上一页
    backPrev:function(){
        wx.navigateBack({
            
        })
    },
    
   
    lookDetail:function(){
      var skuid = this.options.skuid;
      var that = this;

      var option = {
        apiHost: 'https://jd.open.apith.cn/union/',
        api: 'getGoodsDetail',
        data: { id: skuid },
        successCode: 1,
        fn: function (data) {
          that.setData({ goodDetail: data });
        },
      }
      common.request(option);
    },
  buyjd:function(){
    var that=this;
    var skuid = this.options.skuid;
    var couponUrl = this.options.couponUrl;
    console.log(couponUrl);
    var option = {
      apiHost: 'https://jd.open.apith.cn/union/',
      api: 'getCouponCodeByUnionId',
      data: { materialIds: skuid, unionId: 1001031722, couponUrl: couponUrl },
      successCode: 1,
      fn: function (data) {
        common.navToJd(data[0].couponCode);
      },  
    }
    common.request(option);
  },
  
    openQuanBox:function(){
        if (wx.canIUse('web-view')) {
          
            wx.navigateTo({
                 url: "../webview/index",

            })
        } else {
            wx.showModal({
                title:'提示',
                content:'当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试',
                success: function (res) {
                
                }
            })
        }  
    }
    
})